package api

import (
	"blog/core"
	"blog/core/request"
	"github.com/gin-gonic/gin"
	"strconv"
)

type Login struct {
}

// Login , 登录
func (receiver *Login) Login(ctx *gin.Context) {
	req := request.Request(ctx)
	account := req.InputStr("account")
	pwd := req.InputStr("pwd")

	info, err := ServiceGroup.login.Login(account, pwd)
	if err != nil {
		req.Response().Message(err.Error()).Error()
		return
	}

	token, err := LogicGroup.Auth.GetToken(info.ID, 24*60*60)
	if err != nil {
		req.Response().Message(err.Error()).Error()
		return
	}

	go ServiceGroup.login.UpdateLoginLast(info.ID, ctx.ClientIP())
	go ServiceGroup.login.CreateLoginLog(info.ID, ctx.ClientIP())

	req.Response().Message("登录成功").Data(map[string]interface{}{
		"token": token,
	}).Success()
}

// Logout , 退出
func (receiver Login) Logout(req request.InterfaceRequest) {
	id := req.InputInt("id")
	core.Redis.GetString().DelKey("user_jwt_" + strconv.Itoa(id))
}
